$(function(){
	$.post("GetMyOrdersServlet",function(data,status){
		var allOrder=data;
    	var orderStatus;
		var btn;
		
		
		
		
		
		$(allOrder).each(function(index,elem){
			switch(elem.STATUS){
			case -1:orderStatus="<font color='red'>未付款</font>";
				btn="<div class='btn' onclick='toPay("+elem.OrderId+")'><a>去付款</a></div>";
				break;
			case 0:orderStatus="<font color='red'>未发货</font>";
				btn="<div class='btn' onclick='toPay()'><a>去催单</a></div>";
				break;
			case 1:orderStatus="<font color='red'>已发货</font>";
			   btn="<div class='btn' onclick='getGoods("+elem.OrderId+")'><a>收货</a></div>";
			   break;
			case 2:orderStatus="<font color='green'>已收货</font>";
		       btn="<a class='dNone'>可评价</a>";
		      // writeComment="<div class='commentBtn' onclick='writeComment("+_elem.OrderDetailId+")'><a style='text-decoration:underline; color:red;'>去评价</a></div>";
		       break;
			case 3:orderStatus="<font color='green'>已收货</font>";
				btn="<a class='dNone'>评价完毕</a>";
			}
			
			$("#content").append("<table>\
		<thead>\
            <tr>\
                <th>"+elem.CreateDate+"</th>\
                <th><div class='right'>Order ID:&nbsp;</div></th>\
                <th><div class='left'>&nbsp;"+elem.OrderId+"</div></th>\
                <th>Total Price:&nbsp;&nbsp;$"+elem.totalPrice+"</th>\
                <th>"+orderStatus+"</th>\
                <th>"+btn+"</th>\
            </tr>\
        </thead>\
		<tbody id="+elem.OrderId+">\
        </tbody>\
		</table>");
		    $(elem.orderDetailsList).each(function(_index,_elem){
			
		    	var writeComment="";
		    	if(_elem.STATUS==0&&elem.STATUS==2){
		    		 writeComment="<div class='commentBtn' onclick='writeComment("+_elem.OrderDetailId+")'><a style='text-decoration:underline; color:red;'>去评价</a></div>";
		    	}
		    if(_elem.STATUS==1){
		    		 writeComment="<div class='commentBtn'>已评价</div>";
		    	}
		    	
		    	$("#"+elem.OrderId).append("<tr><td><img width='100px' height='154px' src='"+_elem.book.Picture+"'></td>\
                <td>"+_elem.book.BookName+"</td>\
                <td>$"+_elem.book.Price+"</td>\
                <td>&times;"+_elem.quantity+"</td>\
                <td>Total Price:&nbsp;$"+_elem.subTotal+"</td>\
                <td>"+writeComment+"</td></tr>");
		    });
		});
	},"json")
})
function backToHome() {
    window.location.href="customer.jsp";
}
function writeComment(orderDetailId){
	window.location.href="comment.jsp?orderDetailId="+orderDetailId+"";
}
function getGoods(orderId){
	$.ajax({
		data:{orderId:orderId},
		url:"ReceiveGoodsServlet",
		type:"POST",
		success:function(response){
			window.location.reload();
		},
	});
}
function toPay(orderId){
	window.location.href="ToPaymentServlet?orderId="+orderId+"";
}